package com.panopset.gui.selection;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.JToggleButton;

import com.panopset.gui.TPanel;
import com.panopset.java.Version;

/**
 * Selection panel.
 *
 * @author Karl Dinwiddie.
 *
 */
public abstract class SelectionPanel extends TPanel implements ActionListener {

    /**
     * Selection groups.
     */
    private Vector<SelectionGroup> selectionGroups;

    /**
     * Toggle buttons.
     */
    private Vector<JToggleButton> toggleButtons;

    /**
     * Selections.
     */
    private HashMap<String, String> selections;

    /**
     * Key.
     */
    private String key;

    /**
     * Change listener.
     */
    private final Vector<SelectionPanelValueChangeListener> spvcl
     = new Vector<SelectionPanelValueChangeListener>();

    /**
     * Do setup.
     */
    protected abstract void doSetup();

    /**
     * Constructor.
     */
    public SelectionPanel() {

    }

    /**
     * @param initValue Initial value.
     */
    public SelectionPanel(final String initValue) {
        setValueFromString(initValue);
    }

    /**
     * Add selection panel value change listener.
     * @param listener Listener.
     */
    public final void addSelectionPanelValueChangeListener(
            final SelectionPanelValueChangeListener listener) {
        spvcl.add(listener);
    }

    /**
     * @return Selection groups.
     */
    private Vector<SelectionGroup> getSelectionGroups() {
        if (this.selectionGroups == null) {
            this.selectionGroups = new Vector<SelectionGroup>();
        }
        return this.selectionGroups;
    }

    /**
     * Add a selection group.
     * @param group Group to add.
     */
    public final void add(final SelectionGroup group) {
        getSelectionGroups().add(group);
    }

    /**
     * Finalize setup.
     */
    public final void finalizeSetUp() {
        doSetup();
        setLayout(new GridLayout(getSelectionGroups().size(), 1));
        for (SelectionGroup sg : getSelectionGroups()) {
            add(sg);
        }
    }

    /**
     * @param e Action event.
     */
    public final void actionPerformed(final ActionEvent e) {
        for (JToggleButton btn : getToggleButtons()) {
            if (e.getSource() == btn) {
                if (btn.isSelected()) {
                    setKey(btn.getText());
                }
            } else {
                if (btn.isSelected()) {
                    btn.setSelected(false);
                }
            }
        }
    }

    /**
     * @return Selections.
     */
    public final HashMap<String, String> getSelections() {
        if (this.selections == null) {
            this.selections = new HashMap<String, String>();
        }
        return this.selections;
    }

    /**
     * @return toggle button vector.
     */
    public final Vector<JToggleButton> getToggleButtons() {
        if (this.toggleButtons == null) {
            this.toggleButtons = new Vector<JToggleButton>();
        }
        return this.toggleButtons;
    }

    /**
     * @return Selection value.
     */
    public final String getValue() {
        return getSelections().get(getKey());
    }

    /**
     * @return Key.
     */
    public final String getKey() {
        if (key == null) {
            key = "";
        }
        return key;
    }

    /**
     * Set value from String.
     * @param newKey new key value.
     */
    public final void setValueFromString(final String newKey) {
        for (JToggleButton btn : getToggleButtons()) {
            if (btn.getText().equals(newKey)) {
                setKey(newKey);
                btn.setSelected(true);
            } else {
                btn.setSelected(false);
            }
        }

    }

    /**
     * Set key.
     * @param newKey New key.
     */
    private void setKey(final String newKey) {
        this.key = newKey;
        for (SelectionPanelValueChangeListener listener : spvcl) {
            listener.selectionPanelValueHasChangedTo(this.key);
        }
    }

    /**
     * Serial version id.
     */
    private static final long serialVersionUID = Version.UNI_VERSION;
}
